/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __ibmmq_pmo_analysis.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/08 by jikw                                 */
/*******************************************************************/
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frct_comm.h"
#include "../../../src/module/ibmmq/config.h"
#include "../test_ibmmq_comm.h"
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <ibmmq/cnaps2.h>
#include <ibmmq/ibmmq_core.h>

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

static void read_file(char *p_filename, char **p_out, int *p_i_len)
{
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_data = NULL;
    int i_len = 0;

    p_file = fopen(p_filename, "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_data = (char *)frmalloc(i_len + 1);
    memset(p_data, 0x00, i_len + 1);
    fread(p_data, 1, i_len, p_file);
    fclose(p_file);

    *p_out = p_data;
    *p_i_len = i_len;

    return;
}

extern int __ibmmq_pmo_analysis(Frbyte_Packet *p_st_data
                                 , unsigned int *p_ui_data_len
                                 , uint32_t u32_byte_order);

static void case1(void)
{
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = FR_SUCC;
    uint32_t u32_byte_order = 2;

    i_ret = __ibmmq_pmo_analysis(p_st_data, &ui_data_len, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    return;
}

static void case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = FR_SUCC;
    uint32_t u32_byte_order = 2;

    p_st_data = (Frbyte_Packet *)frmalloc(sizeof(Frbyte_Packet));

    i_ret = __ibmmq_pmo_analysis(p_st_data, NULL, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_data);

    return;
}

static void case3(void)
{
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = FR_SUCC;
    char *p_data = NULL;
    int i_len = 0;
    int i_md_idx = 0;
    uint32_t u32_byte_order = 2;

    read_file((char *)"__ibmmq_pmo_analysis3.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    i_ret = bytebuff_memstr(p_st_data, 0, i_len-36, "MD", 2, &i_md_idx);
    bytebuff_skip(p_st_data, i_md_idx);
    bytebuff_skip(p_st_data, 364);
    ui_data_len = 7;

    i_ret = __ibmmq_pmo_analysis(p_st_data, &ui_data_len, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frfree(p_data);

    return;
}

static void case4(void)
{
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = FR_SUCC;
    char *p_data = NULL;
    int i_len = 0;
    int i_md_idx = 0;
    uint32_t u32_byte_order = 2;

    read_file((char *)"__ibmmq_pmo_analysis3.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    i_ret = bytebuff_memstr(p_st_data, 0, i_len-36, "MD", 2, &i_md_idx);
    bytebuff_skip(p_st_data, i_md_idx);
    bytebuff_skip(p_st_data, 364);
    ui_data_len = 8;

    i_ret = __ibmmq_pmo_analysis(p_st_data, &ui_data_len, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frfree(p_data);

    return;
}

static void case5(void)
{
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = FR_SUCC;
    char *p_data = NULL;
    int i_len = 0;
    int i_md_idx = 0;
    uint32_t u32_byte_order = 2;

    read_file((char *)"__ibmmq_pmo_analysis3.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    i_ret = bytebuff_memstr(p_st_data, 0, i_len-36, "MD", 2, &i_md_idx);
    bytebuff_skip(p_st_data, i_md_idx);
    bytebuff_skip(p_st_data, 364);
    ui_data_len = 151;

    i_ret = __ibmmq_pmo_analysis(p_st_data, &ui_data_len, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    bytebuff_free(&p_st_data);
    frfree(p_data);

    return;
}

static void case6(void)
{
    Frbyte_Packet *p_st_data = NULL;
    unsigned int ui_data_len = 0;
    int i_ret = FR_SUCC;
    char *p_data = NULL;
    int i_len = 0;
    int i_md_idx = 0;
    uint32_t u32_byte_order = 2;

    read_file((char *)"__ibmmq_pmo_analysis3.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+36, i_len-36);

    i_ret = bytebuff_memstr(p_st_data, 0, i_len-36, "MD", 2, &i_md_idx);
    bytebuff_skip(p_st_data, i_md_idx);
    bytebuff_skip(p_st_data, 364);
    ui_data_len = i_len-36-i_md_idx-364;

    i_ret = __ibmmq_pmo_analysis(p_st_data, &ui_data_len, u32_byte_order);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(ui_data_len, 28);

    bytebuff_free(&p_st_data);
    frfree(p_data);

    return;
}

int add_suite__ibmmq_pmo_analysis(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __ibmmq_pmo_analysis()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
